package com.cbtx.module_sp;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.provider.Settings;
import android.view.WindowManager;

import com.cbtx.module.common.dialog.CommentConfirmDialog;
import com.cbtx.module_sp.ui.activity.base.MyBaseVMActivity;
import com.txcb.lib.base.AppManager;
import com.txcb.lib.base.utils.LogUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by chenli on 2019-05-23
 */
public class NetWorkChangReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        LogUtil.d("NetWorkChangReceiver: onReceive");

        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isAvailable()) {
//                Toast.makeText(context,"网络已连接",Toast.LENGTH_SHORT).show();

            showNoNet(false);

            EventBus.getDefault().post("closeNetDialog");
//            reGetMainData();

        } else {
//                Toast.makeText(context,"网络断开",Toast.LENGTH_SHORT).show();
            showNoNet(true);
        }

    }


    CommentConfirmDialog commentConfirmDialog;

    private void showNoNet(boolean isShow) {
        if (!isShow) {
            if (commentConfirmDialog != null && commentConfirmDialog.isShowing()) {
                commentConfirmDialog.dismiss();
                commentConfirmDialog = null;
            }
            return;
        }

        if (commentConfirmDialog != null && commentConfirmDialog.isShowing()) {
            return;
        }

        Activity lastActivity = AppManager.getInstance().currentActivity();

        commentConfirmDialog = new CommentConfirmDialog(lastActivity);

        //只有这样才能弹框
        if (Build.VERSION.SDK_INT >= 26) {//8.0新特性
            commentConfirmDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        } else {
            commentConfirmDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        }

        commentConfirmDialog
                .setContent("哎呀，暂无网络，请前往设置")
                .setAgree("前往设置")
                .setOnDialogListener(new CommentConfirmDialog.OnDialogListener() {
                    @Override
                    public void onCancel() {
//                        commentConfirmDialog.dismiss();
                    }

                    @Override
                    public void onAgree() {

                        commentConfirmDialog.dismiss();

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                            Intent panelIntent = new Intent(Settings.Panel.ACTION_WIFI);
                            lastActivity.startActivityForResult(panelIntent, MyBaseVMActivity.RQ_WIFI_SET);

                        } else {
                            Intent intent = new Intent();
                            intent.setAction("android.net.wifi.PICK_WIFI_NETWORK");
                            intent.putExtra(":settings:show_fragment_as_subsetting", true);
                            lastActivity.startActivityForResult(intent, MyBaseVMActivity.RQ_WIFI_SET);
                        }
                    }
                });

        commentConfirmDialog.show();
    }


}
